package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 服务器端向客户端响应的内容类型（MIME）
        resp.setContentType("text/html;charset=utf-8");
        // 设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        //2. 在浏览器页面中输出内容
        PrintWriter out = resp.getWriter();
        //3. 调用service中的方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Guestbook> list = service.getQueryAll();
        //4. 需要将上面获取到的list集合保存到request,之后在list.jsp才能获取数据并显示
        req.setAttribute("list",list);
        //转发资源
        req.getRequestDispatcher("/list.jsp").forward(req,resp);

//        out.println("<table border=\"1\">");
//        out.println("<tr>");
//        out.println("<td>id</td>");
//        out.println("<td>name</td>");
//        out.println("<td>phone</td>");
//        out.println("<td>email</td>");
//        out.println("<td>title</td>");
//        out.println("<td>content</td>");
//        out.println("<td>createdtime</td>");
//        out.println("</tr>");
//        for (int i = 0;i<list.size();i++){
//            Guestbook gb = list.get(i);
//            out.println("<tr>");
//            out.println("<td>"+gb.getId()+"</td>");
//            out.println("<td>"+gb.getName()+"</td>");
//            out.println("<td>"+gb.getPhone()+"</td>");
//            out.println("<td>"+gb.getEmail()+"</td>");
//            out.println("<td>"+gb.getTitle()+"</td>");
//            out.println("<td>"+gb.getContent()+"</td>");
//            out.println("<td>"+gb.getCreatedtime()+"</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
